print("202501141715 - 打印 ReplicatedStorage.Data.PetRegistry 全量数据")

local HttpService = game:GetService("HttpService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local function log(s)
	local line = string.format("[%s] %s", os.date("%Y-%m-%d %H:%M:%S"), tostring(s))
	print("[PetRegistry] " .. tostring(s))
	local ok = pcall(function()
		appendfile("pet_registry_log.txt", line .. "\n")
	end)
	if not ok then
		pcall(function()
			writefile("pet_registry_log.txt", line .. "\n")
		end)
	end
end

local function safeToString(v)
	local ok, res = pcall(function() return tostring(v) end)
	return ok and res or "[unstringifiable]"
end

local function deepCopySample(tbl, depth, maxDepth, maxItems)
	depth = depth or 0
	maxDepth = maxDepth or 6
	maxItems = maxItems or 2000
	if typeof(tbl) ~= "table" then return tbl end
	if depth >= maxDepth then return "[maxDepth]" end
	local out = {}
	local count = 0
	for k, v in pairs(tbl) do
		count = count + 1
		if count > maxItems then
			out["__truncated__"] = true
			break
		end
		if typeof(v) == "table" then
			out[safeToString(k)] = deepCopySample(v, depth + 1, maxDepth, maxItems)
		else
			out[safeToString(k)] = v
		end
	end
	return out
end

local function findPath(root, segments)
	local node = root
	for _, name in ipairs(segments) do
		node = node:FindFirstChild(name)
		if not node then return nil, "未找到: " .. table.concat(segments, ".") end
	end
	return node
end

local function exportFromModuleScript(modScript)
	local ok, mod = pcall(function() return require(modScript) end)
	if not ok then
		log("❌ require失败: " .. safeToString(mod))
		return false
	end
	local snapshot
	if typeof(mod) == "table" then
		snapshot = deepCopySample(mod, 0, 10, 20000)
	else
		snapshot = { __warn = "module returned non-table", kind = typeof(mod), value = safeToString(mod) }
	end
	local okJson, encoded = pcall(function() return HttpService:JSONEncode(snapshot) end)
	if okJson then
		pcall(function()
			writefile("pet_registry.json", encoded)
			log("✅ 已导出 pet_registry.json")
		end)
	else
		log("⚠️ JSON 编码失败")
	end
	-- 顶层键预览
	local lines = { "=== PetRegistry 顶层键 ===" }
	local top = 0
	if typeof(mod) == "table" then
		for k, v in pairs(mod) do
			top = top + 1
			local t = typeof(v)
			table.insert(lines, string.format("%d. %s (%s)", top, safeToString(k), t))
			if t == "table" then
				local sub = 0
				for sk in pairs(v) do
					sub = sub + 1
					if sub <= 10 then table.insert(lines, "   - " .. safeToString(sk)) end
				end
				if sub > 10 then
					table.insert(lines, string.format("   ...还有 %d 个键", sub - 10))
				end
			end
		end
	end
	pcall(function()
		writefile("pet_registry_keys.txt", table.concat(lines, "\n"))
		log("✅ 已导出 pet_registry_keys.txt")
	end)
	return true
end

local function exportFromInstanceTree(node)
	-- 将实例树转为可序列化结构（仅名称/类型/子项）
	local function toTree(inst, depth, maxDepth, maxKids)
		depth = depth or 0
		maxDepth = maxDepth or 6
		maxKids = maxKids or 2000
		local obj = { name = inst.Name, className = inst.ClassName, children = {} }
		if depth >= maxDepth then return obj end
		local kids = inst:GetChildren()
		local n = 0
		for _, c in ipairs(kids) do
			n = n + 1
			if n > maxKids then break end
			obj.children[#obj.children+1] = toTree(c, depth + 1, maxDepth, maxKids)
		end
		return obj
	end
	local tree = toTree(node, 0, 10, 10000)
	local okJson, encoded = pcall(function() return HttpService:JSONEncode(tree) end)
	if okJson then
		pcall(function()
			writefile("pet_registry_tree.json", encoded)
			log("✅ 已导出 pet_registry_tree.json")
		end)
	end
	return true
end

local function main()
	log("🚀 开始打印 ReplicatedStorage.Data.PetRegistry ...")
	local node, err = findPath(ReplicatedStorage, { "Data", "PetRegistry" })
	if not node then
		log("❌ " .. tostring(err))
		return false
	end
	if node:IsA("ModuleScript") then
		return exportFromModuleScript(node)
	else
		log("ℹ️ 目标不是ModuleScript，作为实例树导出")
		return exportFromInstanceTree(node)
	end
end

main()
